﻿<!DOCTYPE html>
<html>
<head>
	<title>ol-ext: Convex hull</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<meta name="description" content="Calculate the convex hull of a set of points." />
	<meta name="keywords" content="ol3, geom, polygon, convex hull, envelope" />

	<!-- jQuery -->
	<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

	<!-- Openlayers -->
    <link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" />
	<script type="text/javascript" src="https://openlayers.org/en/latest/build/ol.js"></script>
	<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,Object.assign"></script>
	
	<!-- ol-ext -->
    <link rel="stylesheet" href="../../dist/ol-ext.css" />
	<script type="text/javascript" src="../../dist/ol-ext.js"></script>

	<link rel="stylesheet" href="../style.css" />
</head>
<body >
	<a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

	<a href="../../index.html">
		<h1>ol-ext: Convex hull</h1>
	</a>
	<div class="info">
		The <i>ol.coordinate.convexHull()</i> function calculate a <a href="https://en.wikipedia.org/wiki/Convex_hull">convex hull</a>
		or convex envelope, ie. the smallest polygon that contains all points of a set.
		<br/>
		It uses the <a href="https://en.wikipedia.org/wiki/Convex_hull_algorithms#Algorithms">Andrew's Monotone Chain Algorithm</a> to complete the hull.
		<p>
			It can be used to display a polygon <a href="map.cluster.convexhull.html">around the points of a cluster</a>.
		</p>
	</div>

	<!-- Map div -->
	<div id="map" style="width:600px; height:400px;"></div>
	
	<div class="options">
		Click on the map to add features and calculate the convex hull.
		<ul><li>
				<button class="button" onclick="vector.getSource().clear();">Clear!</button>
		</li></ul>
	</div>

	<script type="text/javascript">

	// Two base layers
	var stamen = new ol.layer.Tile(
		{	title: "Watercolor",
			baseLayer: true,
			source: new ol.source.Stamen({
				layer: 'watercolor'
			})
		});

	// The map
	var map = new ol.Map
		({	target: 'map',
			view: new ol.View
			({	zoom: 4,
				center: [205461, 5867916]
			}),
			layers: [stamen]
		});

	// Add over interaction that draw hull in a layer
	var vector = new ol.layer.Vector({ source: new ol.source.Vector() })
	map.addLayer(vector);

	var draw = new ol.interaction.Draw({ source:vector.getSource(), type:"Point" });
	map.addInteraction(draw);

	var hull, pts;
	draw.on("drawend", function(e)
	{	if (!vector.getSource().getFeatures().length)
		{	hull = new ol.Feature(new ol.geom.Polygon([[0,0]]));
			vector.getSource().addFeature(hull);
			pts = [];
		}
		pts.push(e.feature.getGeometry().getCoordinates());
		hull.setGeometry(new ol.geom.Polygon ( [ ol.coordinate.convexHull(pts) ] ));
	});

	</script>
	
</body>
</html>